/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triploa;
import org.kxml2.io.*;
import org.kxml2.kdom.*;
import org.xmlpull.v1.*;
import java.io.*;

/**
 *
 * @author Edoardo
 */
public class Stage {
    private static final String STAGE_TAG="a:StageInfo";
    private static final String FROM_TAG="a:Origin";
    private static final String TO_TAG="a:Destination";
    private static final String ADDRESS_TAG="a:Address";
    private static final String DISTANCE_TAG="a:Distance";
    private static final String TIME_TAG="a:EstimatedTime";
    private static final String COST_TAG="a:EstimatedBudget";
    private static final String BUSY_SEATS_TAG="a:BusySeats";
    private static final String COORDS_TAG="a:Coords";
   
    private String from=null;
    private String to=null;
    private String cost=null;
    private String time=null;
    private String distance=null;
    private Vehicle vehicle=null;
    /**
 * call for getting a new instance of this class from xml
 * @param p Parser
 * @return a new instance of FeedBack
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public static Stage create(KXmlParser p) throws XmlPullParserException, IOException {
        Stage up = new Stage();
        up.update(p);
        if(up.getFrom()==null&&up.getTo()==null) return null;
        return up;
    }
    /**
 * Update this object class from xml parser
 * @param p Parser
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public void update(KXmlParser p) throws XmlPullParserException, IOException {
        boolean exit = false, travel = false,destination=false,origin=false;
        int token = -1;
        String n="",lasttag=n;
        while (!exit) {
            token = p.next();
            switch (token) {
                case KXmlParser.START_DOCUMENT:
                    break;
                case KXmlParser.END_DOCUMENT:
                    exit = true;  //exit form while
                    break;
                case KXmlParser.START_TAG:
                    System.out.println(p.getName());
                    if (p.getName().equals(STAGE_TAG)) {
                        travel = true;
                    }
                    if (travel) {
                        lasttag=p.getName();
                        if (lasttag.equals(FROM_TAG)) {
                            origin=true;
                            destination=false;
                        }
                        if (lasttag.equals(TO_TAG)) {
                            destination=true;
                            origin=false;
                        }
                    }
                    break;
                case KXmlParser.END_TAG:
                    if (travel) {
                        if (p.getName().equals(STAGE_TAG)) {
                            travel = false;
                            exit = true;
                            break;
                        }
                        if(p.getName().equals(this.TO_TAG))
                        {
                            setVehicle(Vehicle.create(p));
                        }
                    }
                    lasttag=n;
                    break;
                case KXmlParser.TEXT:
                    if (lasttag.equals(DISTANCE_TAG)) {
                        distance = p.getText();
                    }
                    
                    if (lasttag.equals(TIME_TAG)) {
                        time= p.getText();
                    }
                    
                    if (lasttag.equals(COST_TAG)) {
                        cost = p.getText();
                    }
                    if (lasttag.equals(ADDRESS_TAG)) {
                        if(origin) from=p.getText();
                        else if(destination) to=p.getText();
                    }
                    break;
            }
        }
    }

    public

    String getFrom() {
        return from;
    }

    public String getTo() {
        return to;
    }

    public String getCost() {
        return cost;
    }

    public String getTime() {
        return time;
    }

    public String getDistance() {
        return distance;
    }

    public Vehicle getVehicle() {
        return vehicle;
    }

    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle;
    }
}
